package bio.file.serializeStream;

import org.junit.Test;

import java.io.*;

/**
 * Instruction：
 * Author：@author MaLi
 */
public class SaveObject {
    @Test
    public void copy() {
        Some some = new Some(1, "001");
        try(ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("some.txt"));
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("some.txt"));) {
            oos.writeObject(some);
            oos.flush();
            Some some2 = (Some) ois.readObject();
            System.out.println(some2);
        } catch (IOException e) {
            throw new RuntimeException(e);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }

    public static class Some implements Serializable {
        private int number;
        private String name;

        public Some(int number, String name) {
            this.number = number;
            this.name = name;
        }

        public int getNumber() {
            return number;
        }

        public void setNumber(int number) {
            this.number = number;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        @Override
        public String toString() {
            return "Some{" +
                    "number=" + number +
                    ", name='" + name + '\'' +
                    '}';
        }
    }
}
